home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2000 July
/
CHIP Turkiye Temmuz 2000.iso
/
prog
/
share
/
20
/
20.exe
/
COMPLEX.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
2000-04-04
|
12KB
|
388 lines
Unit Complex;
Interface
Uses SysUtils, Math;
Type
TComplex = record
real: Extended;
imag: Extended;
end;
Function MakeComplex(const real, imag: Extended): TComplex;
Procedure SetResult(var x,y: Extended; const complex: TComplex);
Procedure CAddV(var Cmp1: TComplex; const Cmp2: TComplex); // Z:=Z+A
Function CAdd (const Cmp1, Cmp2: TComplex): TComplex; // V:=Z+A
Procedure CSubV(var Cmp1: TComplex; const Cmp2: TComplex); // Z:=Z-A
Function CSub (const Cmp1, Cmp2: TComplex): TComplex; // V:=Z-A
Procedure CMulV(var Cmp1: TComplex; const Cmp2: TComplex); // Z:=Z*A
Function CMul (const Cmp1, Cmp2: TComplex): TComplex; // V:=Z*A
Procedure CDivV(var Cmp1: TComplex; const Cmp2: TComplex); // Z:=Z/A
Function CDiv (const Cmp1, Cmp2: TComplex): TComplex; // V:=Z/A
Procedure CSqrV(var Cmp1: TComplex); // Z:=Z*Z
Function CSqr (const Cmp1: TComplex): TComplex; // V:=Z*Z
Procedure CTripleV(var Cmp1: TComplex); // Z:=Z*Z*Z
Function CTriple (const Cmp1: TComplex): TComplex; // V:=Z*Z*Z
Procedure CFourV(var Cmp1: TComplex); // Z:=Z*Z*Z*Z
Function CFour (const Cmp1: TComplex): TComplex; // V:=Z*Z*Z*Z
Procedure CFlipV(var Cmp1: TComplex);
Function CFlip (const Cmp1: TComplex): TComplex;
Procedure CRevV (var Cmp1: TComplex); // Z:=1/Z
Function CRev (const Cmp1: TComplex): TComplex; // V:=1/Z
Procedure CRev2V(var Cmp1: TComplex; const Cmp2: TComplex); // Z:=1/(Z-A)
Function CRev2 (const Cmp1,Cmp2: TComplex): TComplex; // V:=1/(Z-A)
Procedure CSqrtV(var Cmp1: TComplex); // Z:=Sqrt(Z)
Function CSqrt (const Cmp1: TComplex): TComplex; // V:=Sqrt(Z)
Procedure CExpV (var Cmp1: TComplex); // Z:=Exp(Z)
Function CExp (const Cmp1: TComplex): TComplex; // V:=Exp(Z)
Procedure CLnV (var Cmp1: TComplex); // Z:=Ln(Z)
Function CLn (const Cmp1: TComplex): TComplex; // V:=Ln(Z)
Procedure CPowerV(var Cmp1: TComplex; Cmp2: TComplex); // Z:=Z^A
Function CPower (const Cmp1,Cmp2: TComplex): TComplex; // V:=Z^A
Procedure CSinV (var Cmp1: TComplex); // Z:=Sin(Z)
Function CSin (const Cmp1: TComplex): TComplex; // V:=Sin(Z)
Procedure CCosV (var Cmp1: TComplex); // Z:=Cos(Z)
Function CCos (const Cmp1: TComplex): TComplex; // V:=Cos(Z)
Procedure CTanV (var Cmp1: TComplex); // Z:=Tan(Z)
Function CTan (const Cmp1: TComplex): TComplex; // V:=Tan(Z)
Procedure CSinhV(var Cmp1: TComplex); // Z:=Sinh(Z)
Function CSinh (const Cmp1: TComplex): TComplex; // V:=Sinh(Z)
Procedure CCoshV(var Cmp1: TComplex); // Z:=Cosh(Z)
Function CCosh (const Cmp1: TComplex): TComplex; // V:=Cosh(Z)
Implementation
Const SmallTol: Extended = 1E-25;
Function MakeComplex(const real, imag: Extended): TComplex;
Begin
Result.real:=real;
Result.imag:=imag;
End;
Procedure SetResult(var x,y: Extended; const complex: TComplex);
Begin
x:=complex.real;
y:=complex.imag;
End;
{ ------------------------------------------------------------------- }
Procedure CAddV(var Cmp1: TComplex; const Cmp2: TComplex);
Begin
Cmp1.real:=Cmp1.real+Cmp2.real;
Cmp1.imag:=Cmp1.imag+Cmp2.imag;
End;
Function CAdd (const Cmp1, Cmp2: TComplex): TComplex;
Begin
Result.real:=Cmp1.real+Cmp2.real;
Result.imag:=Cmp1.imag+Cmp2.imag;
End;
Procedure CSubV(var Cmp1: TComplex; const Cmp2: TComplex);
Begin
Cmp1.real:=Cmp1.real-Cmp2.real;
Cmp1.imag:=Cmp1.imag-Cmp2.imag;
End;
Function CSub (const Cmp1, Cmp2: TComplex): TComplex;
Begin
Result.real:=Cmp1.real-Cmp2.real;
Result.imag:=Cmp1.imag-Cmp2.imag;
End;
Procedure CMulV(var Cmp1: TComplex; const Cmp2: TComplex);
var tmp: Extended;
Begin
tmp :=Cmp1.real*Cmp2.real - Cmp1.imag*Cmp2.imag;
Cmp1.imag:=Cmp1.real*Cmp2.imag + Cmp1.imag*Cmp2.real;
Cmp1.real:=tmp;
End;
Function CMul (const Cmp1, Cmp2: TComplex): TComplex;
Begin
Result.real:=Cmp1.real*Cmp2.real - Cmp1.imag*Cmp2.imag;
Result.imag:=Cmp1.real*Cmp2.imag + Cmp1.imag*Cmp2.real;
End;
Procedure CDivV(var Cmp1: TComplex; const Cmp2: TComplex);
var tmp1,tmp2: Extended;
Begin
tmp1 := Cmp2.real*Cmp2.real + Cmp2.imag*Cmp2.imag + SmallTol;
tmp2 :=(Cmp1.real*Cmp2.real + Cmp1.imag*Cmp2.imag)/tmp1;
Cmp1.imag :=(Cmp1.imag*Cmp2.real - Cmp1.real*Cmp2.imag)/tmp1;
Cmp1.real := tmp2;
End;
Function CDiv (const Cmp1, Cmp2: TComplex): TComplex;
var tmp: Extended;
Begin
tmp := Cmp2.real*Cmp2.real + Cmp2.imag*Cmp2.imag + SmallTol;
Result.real:=(Cmp1.real*Cmp2.real + Cmp1.imag*Cmp2.imag)/tmp;
Result.imag:=(Cmp1.imag*Cmp2.real - Cmp1.real*Cmp2.imag)/tmp;
End;
{ ------------------------------------------------------------------- }
Procedure CSqrV(var Cmp1: TComplex);
var tmp: Extended;
Begin
tmp :=(Cmp1.real+Cmp1.imag)*(Cmp1.real-Cmp1.imag);
Cmp1.imag:= Cmp1.real*Cmp1.imag * 2;
Cmp1.real:=tmp;
End;
Function CSqr (const Cmp1: TComplex): TComplex;
Begin
Result.real:=(Cmp1.real+Cmp1.imag)*(Cmp1.real-Cmp1.imag);
Result.imag:= Cmp1.real*Cmp1.imag * 2;
End;
Procedure CTripleV(var Cmp1: TComplex);
var tmp: Extended;
Begin
tmp :=Cmp1.real*(Cmp1.real*Cmp1.real - 3*Cmp1.imag*Cmp1.imag);
Cmp1.imag:=Cmp1.imag*(3*Cmp1.real*Cmp1.real - Cmp1.imag*Cmp1.imag);
Cmp1.real:=tmp;
End;
Function CTriple (const Cmp1: TComplex): TComplex;
Begin
Result.real:=Cmp1.real*(Cmp1.real*Cmp1.real - 3*Cmp1.imag*Cmp1.imag);
Result.imag:=Cmp1.imag*(3*Cmp1.real*Cmp1.real - Cmp1.imag*Cmp1.imag);
End;
Procedure CFourV(var Cmp1: TComplex);
var tmpR, tmpI: Extended;
Begin
tmpR:=(Cmp1.real-Cmp1.imag)*(Cmp1.real+Cmp1.imag);
tmpI:= Cmp1.real*Cmp1.imag * 2;
Cmp1.real:=(tmpR-tmpI)*(tmpR+tmpI);
Cmp1.imag:= tmpR*tmpI * 2;
End;
Function CFour (const Cmp1: TComplex): TComplex;
var tmpR, tmpI: Extended;
Begin
tmpR:=(Cmp1.real-Cmp1.imag)*(Cmp1.real+Cmp1.imag);
tmpI:= Cmp1.real*Cmp1.imag * 2;
Result.real:=(tmpR-tmpI)*(tmpR+tmpI);
Result.imag:= tmpR*tmpI * 2;
End;
{ ------------------------------------------------------------------- }
Procedure CFlipV(var Cmp1: TComplex);
var tmp: Extended;
Begin
tmp:=Cmp1.real;
Cmp1.real:=Cmp1.imag;
Cmp1.imag:=tmp;
End;
Function CFlip (const Cmp1: TComplex): TComplex;
Begin
Result.real:=Cmp1.imag;
Result.imag:=Cmp1.real;
End;
Procedure CRevV(var Cmp1: TComplex);
var tmp: Extended;
Begin
tmp := Cmp1.real*Cmp1.real + Cmp1.imag*Cmp1.imag + SmallTol;
Cmp1.real := Cmp1.real/tmp;
Cmp1.imag :=-Cmp1.imag/tmp;
End;
Function CRev (const Cmp1: TComplex): TComplex;
var tmp: Extended;
Begin
tmp := Cmp1.real*Cmp1.real + Cmp1.imag*Cmp1.imag + SmallTol;
Result.real:= Cmp1.real/tmp;
Result.imag:=-Cmp1.imag/tmp;
End;
Procedure CRev2V(var Cmp1: TComplex; const Cmp2: TComplex);
var tmp: Extended;
Begin
tmp := (Cmp1.real-Cmp2.real)*(Cmp1.real-Cmp2.real) +
(Cmp1.imag-Cmp2.imag)*(Cmp1.imag-Cmp2.imag) + SmallTol;
Cmp1.real := (Cmp1.real-Cmp2.real)/tmp;
Cmp1.imag := (Cmp2.imag-Cmp1.imag)/tmp;
End;
Function CRev2 (const Cmp1,Cmp2: TComplex): TComplex;
var tmp: Extended;
Begin
tmp := (Cmp1.real-Cmp2.real)*(Cmp1.real-Cmp2.real) +
(Cmp1.imag-Cmp2.imag)*(Cmp1.imag-Cmp2.imag) + SmallTol;
Result.real:= (Cmp1.real-Cmp2.real)/tmp;
Result.imag:= (Cmp2.imag-Cmp1.imag)/tmp;
End;
{ ------------------------------------------------------------------- }
Procedure CSqrtV(var Cmp1: TComplex); // Z:=Sqrt(Z)
var tmp,tmp2: Extended;
Begin
tmp :=Sqrt(Cmp1.real*Cmp1.real+Cmp1.imag*Cmp1.imag);
tmp2:=Sqrt((Cmp1.real+tmp)/2);
Cmp1.imag:=Sqrt((tmp-Cmp1.real)/2);
Cmp1.real:=tmp2;
End;
Function CSqrt (const Cmp1: TComplex): TComplex; // V:=Sqrt(Z)
var tmp: Extended;
Begin
tmp :=Sqrt(Cmp1.real*Cmp1.real+Cmp1.imag*Cmp1.imag);
Result.real:=Sqrt((Cmp1.real+tmp)/2);
Result.imag:=Sqrt((tmp-Cmp1.real)/2);
End;
Procedure CExpV (var Cmp1: TComplex); // Z:=Exp(Z)
var tmp: Extended;
Begin
tmp :=Exp(Cmp1.real);
Cmp1.real:=tmp*Cos(Cmp1.imag);
Cmp1.imag:=tmp*Sin(Cmp1.imag);
End;
Function CExp (const Cmp1: TComplex): TComplex; // V:=Exp(Z)
var tmp: Extended;
Begin
tmp :=Exp(Cmp1.real);
Result.real:=tmp*Cos(Cmp1.imag);
Result.imag:=tmp*Sin(Cmp1.imag);
End;
Procedure CLnV (var Cmp1: TComplex); // Z:=Ln(Z)
var tmp: Extended;
Begin
tmp :=Log2(Cmp1.real*Cmp1.real + Cmp1.imag*Cmp1.imag)/2.7182818285;
Cmp1.imag:=ArcTan2(Cmp1.imag, Cmp1.real);
Cmp1.real:=tmp;
End;
Function CLn (const Cmp1: TComplex): TComplex; // V:=Ln(Z)
Begin
Result.real:=Log2(Cmp1.real*Cmp1.real + Cmp1.imag*Cmp1.imag)/2.7182818285;
Result.imag:=ArcTan2(Cmp1.imag, Cmp1.real);
End;
Procedure CPowerV(var Cmp1: TComplex; Cmp2: TComplex); // Z:=Z^A
var t: TComplex;
Begin
t:=CLn(Cmp1);
t:=CMul(Cmp1,Cmp2);
Cmp1:=CExp(t);
End;
Function CPower (const Cmp1,Cmp2: TComplex): TComplex; // V:=Z^A
var t: TComplex;
Begin
t:=CLn(Cmp1);
t:=CMul(Cmp1,Cmp2);
Result:=CExp(t);
End;
{ ------------------------------------------------------------------- }
Procedure CSinV (var Cmp1: TComplex); // Z:=Sin(Z)
var tmp1,tmp2,tmp3: Extended;
Begin
tmp1:=Exp(Cmp1.imag)/2;
tmp2:=0.25/tmp1;
tmp3:=Sin(Cmp1.real)*(tmp1+tmp2);
Cmp1.imag:=Cos(Cmp1.real)*(tmp1-tmp2);
Cmp1.real:=tmp3;
End;
Function CSin (const Cmp1: TComplex): TComplex; // V:=Sin(Z)
var tmp1,tmp2: Extended;
Begin
tmp1:=Exp(Cmp1.imag)/2;
tmp2:=0.25/tmp1;
Result.real:=Sin(Cmp1.real)*(tmp1+tmp2);
Result.imag:=Cos(Cmp1.real)*(tmp1-tmp2);
End;
Procedure CCosV (var Cmp1: TComplex); // Z:=Cos(Z)
var tmp1: Extended;
Begin
tmp1:=Cos(Cmp1.real)*Cosh(Cmp1.imag);
Cmp1.imag:=-Sin(Cmp1.real)*Sinh(Cmp1.imag);
Cmp1.real:=tmp1;
End;
Function CCos (const Cmp1: TComplex): TComplex; // V:=Cos(Z)
Begin
Result.real:=Cos(Cmp1.real)*Cosh(Cmp1.imag);
Result.imag:=-Sin(Cmp1.real)*Sinh(Cmp1.imag);
End;
Procedure CTanV (var Cmp1: TComplex); // Z:=Tan(Z)
var tmp: Extended;
Begin
tmp :=Sin(2*Cmp1.real)/(Cos(2*Cmp1.real)+Cosh(2*Cmp1.imag));
Cmp1.imag:=Sin(2*Cmp1.imag)/(Cos(2*Cmp1.real)+Cosh(2*Cmp1.imag));
Cmp1.real:=tmp;
End;
Function CTan (const Cmp1: TComplex): TComplex; // V:=Tan(Z)
Begin
Result.real:=Sin(2*Cmp1.real)/(Cos(2*Cmp1.real)+Cosh(2*Cmp1.imag));
Result.imag:=Sin(2*Cmp1.imag)/(Cos(2*Cmp1.real)+Cosh(2*Cmp1.imag));
End;
Procedure CSinhV(var Cmp1: TComplex); // Z:=Sinh(Z)
var tmp1: Extended;
Begin
tmp1:=Sinh(Cmp1.real)*Cos(Cmp1.imag);
Cmp1.imag:=Cosh(Cmp1.real)*Sin(Cmp1.imag);
Cmp1.real:=tmp1;
End;
Function CSinh (const Cmp1: TComplex): TComplex; // V:=Sinh(Z)
Begin
Result.real:=Sinh(Cmp1.real)*Cos(Cmp1.imag);
Result.imag:=Cosh(Cmp1.real)*Sin(Cmp1.imag);
End;
Procedure CCoshV(var Cmp1: TComplex); // Z:=Cosh(Z)
var tmp1: Extended;
Begin
tmp1:=Cosh(Cmp1.real)*Cos(Cmp1.imag);
Cmp1.imag:=Sinh(Cmp1.real)*Sin(Cmp1.imag);
Cmp1.real:=tmp1;
End;
Function CCosh (const Cmp1: TComplex): TComplex; // V:=Cosh(Z)
Begin
Result.real:=Cosh(Cmp1.real)*Cos(Cmp1.imag);
Result.imag:=Sinh(Cmp1.real)*Sin(Cmp1.imag);
End;
END.